<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: search.php Sat Jul 18 12:38:53 CST 2009
 */

include "conf.inc";

$op = isset($_GET["op"]) ? trim($_GET["op"]) : "group";
$cat_id = isset($_GET["cat_id"]) ? intval($_GET["cat_id"]) : 0 ;
$start = isset($_GET["start"]) ? intval($_GET["start"]) : 0 ;
$_key = isset($_GET["key"]) ? trim($_GET["key"]) : "" ;
$extra = "op={$op}";
$limit = 20;
$_keylen = 4;
$uid = ( !empty($xoopsUser) && is_object($xoopsUser) ) ? $xoopsUser->getVar("uid") : 0;

/** 加载相关的类文件 **/
$myts = MyTextSanitizer::getInstance();
$category_handler = xoops_getmodulehandler("category");
$group_handler = xoops_getmodulehandler("group");
$threads_handler = xoops_getmodulehandler("threads");
$column_handler = xoops_getmodulehandler("column");

$temp_queries = !empty($_key) ? preg_split("/[\s,|]+/", $_key) : array();

switch ($op) {
    case "topic":
        /** 加载模板文件 **/
        $xoopsOption["xoops_pagetitle"] = $xoopsModuleConfig["groupstitle"] . " - ". _SEARCH;
        $xoopsOption["template_main"] = "groups_search_topic.html";
        include "header.php";
        
        /** 组合条件 **/
        $_list = $_queries = array();
        $_queries[] = $temp_queries ? "" : _GP_SEARCH_INOFKEY;
        $_topics_total = 0;
        $limit = 10;
        if ( strlen($_key) >= $_keylen ) {
            $_group_ids = array();
            if ( !empty($cat_id) ) {
                $criteria = new CriteriaCompo(new Criteria("cat_id",$cat_id));
                $_group_ids = $groups_handler->getIds($criteria);
                unset($criteria);
                $extra .= "&cat_id={$cat_id}";
            }
            
            $criteria = new CriteriaCompo();
            if ( $_group_ids ) {
                if ( count($_group_ids) > 30 ) {
                    $_group_ids = array_rand($_group_ids,30);
                }
                $criteria->add(new Criteria("group_id",implodeArrayforSql($_group_ids),"in"));
            }
            if ( $temp_queries ) {
                unset($_queries["0"]);
                $criteria2 = new CriteriaCompo();
                foreach ( $temp_queries as $q ) {
                    $q = trim($q);
                    if (strlen($q) >= $_keylen) {
                        $_q = $myts->addSlashes($q);
                        $criteria2->add(new Criteria("threads_subject","%{$_q}%","LIKE"),"OR");
                        $_queries[] = $q;
                    }
                }
                $extra .= "&key={$_key}";
            } 
            $criteria->add($criteria2);
            $criteria->setStart($start);
            $criteria->setLimit($limit);
            $criteria->setOrder("DESC");
            $criteria->setSort("threads_lastdate");
            $_topics_total = $threads_handler->getCount($criteria);
            
            if( $_topics_total > $limit){	
            	$nav = new XoopsPageNav($_topics_total, $limit, $start, "start",$extra);
            	$xoopsTpl->assign("pagenav", $nav->renderNav(4));
            }
            
            $_topics_objs = $threads_handler->getAll($criteria);
            $_column_ids = array();
            if ( $_topics_objs ) {
                foreach ( $_topics_objs as $k=>$obj ) {
                    if ( $obj->getVar("column_id") ) {
                        $_column_ids[] = $obj->getVar("column_id");
                    }
                    $_uids[] = $obj->getVar("uid");
                }
                if ( $_uids ) {
                    $_uids = array_unique($_uids);
                    $member_handler = xoops_gethandler("member");
        	    	$criteria = new CriteriaCompo(new Criteria("uid",implodeArrayforSql($_uids),"in"));
        	    	$_users = $member_handler->getUsers($criteria);
        	    	
        	    	$avatar = $u = array();
        			foreach ( $_users as $k=>$user ) {
        				$avatar[$user->getVar("uid")] = $user->user_avatar();
        				$u[$user->getVar("uid")] = $user->name();
        			}
        			
        			$online_handler =& xoops_gethandler("online");
        			$criteria = new CriteriaCompo(new Criteria("online_uid",implodeArrayforSql($_uids),"in"));
        			$_onlines = $online_handler->getAll($criteria,null,false,"online_uid");
        			$onlines = array();
        			if ( $_onlines ) {
        				foreach ( $_onlines as $k=>$online ) {
        					$onlines[$online["online_uid"]] = true;
        				}
        			}
                }
                if ( $_column_ids ) {
                    $_column_ids = array_unique($_column_ids);
                    
                    $criteria = new CriteriaCompo(new Criteria("column_id",implodeArrayforSql($_column_ids),"in"));
                    $_columns = $column_handler->getAll($criteria,null,false);
                    $columns = array();
                    if ( $_columns ) {
                    	foreach ( $_columns as $k=>$column ) {
                    		$columns[$k] = $column["column_name"];
                    	}
                    }
                }
                
                foreach ( $_topics_objs as $k=>$obj ) {
                    $_list[$k] = $obj->getValues();
                    $_list[$k]["dateline"] = $obj->dateLineAll();
                    $_list[$k]["author"] = !empty($u[$obj->getVar("uid")]) ? $u[$obj->getVar("uid")] : $obj->getVar("author");
    				$_list[$k]["author_avatar"] = $avatar[$obj->getVar("uid")];
    				$_list[$k]["online"] = !empty($onlines[$obj->getVar("uid")]) ? "<img src=\"images/online_1.gif\" alt=\"Online\" />" : "";
    				$_list[$k]["column_name"] = $obj->getVar("column_id") ? groups_getTopicHighlight($obj->getVar("threads_highlight"),$columns[$obj->getVar("column_id")]) : "";
    				$_list[$k]["subject"] = filterString($obj->getVar("threads_subject"),$_queries);
                }
                
            }
        }
        $xoopsTpl->assign(array("topics"=>$_list,"search_total"=>sprintf(_GP_SEARCH_TOTAL,$_topics_total),"search_keys"=>$_queries));
    break;
    
    case "group":
    default:
        /** 加载模板文件 **/
        $xoopsOption["xoops_pagetitle"] = $xoopsModuleConfig["groupstitle"] . " - ". _SEARCH;
        $xoopsOption["template_main"] = "groups_search_group.html";
        include "header.php";
                
        /** 组合条件 **/
        $_list = $_queries = array();
        $_queries[] = $temp_queries ? "" : _GP_SEARCH_INOFKEY;
        $_group_total = 0;
        if ( strlen($_key) >= $_keylen ) {
            $criteria = new CriteriaCompo();
            if ( !empty($cat_id) ) {
                $criteria->add(new Criteria("cat_id",$cat_id),"AND");
                $extra .= "&cat_id={$cat_id}";
            }
            
            if ( $temp_queries ) {
                unset($_queries["0"]);
                $criteria2 = new CriteriaCompo();
                foreach ( $temp_queries as $q ) {
                    if ( strlen($q) >= $_keylen ) {
                        $criteria2->add(new Criteria("group_name","%{$q}%","LIKE"),"OR");
                        $_queries[] = $q;
                    }
                }
                $extra .= "&key={$_key}";
            } 
            $criteria->add($criteria2);
            $criteria->setStart($start);
            $criteria->setLimit($limit);
            $criteria->setOrder("DESC");
            $criteria->setSort("group_foot_total");
            $_group_total = $group_handler->getCount($criteria);
            
            if( $_group_total > $limit){	
            	$nav = new XoopsPageNav($_group_total, $limit, $start, "start",$extra);
            	$xoopsTpl->assign("pagenav", $nav->renderNav(4));
            }
            
            $_group_objs = $group_handler->getAll($criteria);
            
            if ( $_group_objs ) {
                $_uids = array();
                foreach ( $_group_objs as $key=>$group_obj ) {
            		$_uids[] = $group_obj->getVar("uid");
            	}
            	
                if ( $_uids ) {
                    $_uids = array_unique($_uids);
                    $member_handler = xoops_gethandler("member");
                	$criteria = new CriteriaCompo(new Criteria("uid", " ( " . implode(",",$_uids) . " ) ", "in"));
                	$users = $member_handler->getUsers($criteria);
                	$u = array();
                	foreach ( array_keys($users) as $key ) {
                		$u[$users[$key]->getVar("uid")] = $users[$key]->name();
                	}
                }
                
                foreach ( $_group_objs as $k=>$_obj ) {
                    $_list[$k] = $_obj->getValues();
                    $_list[$k]["creator"] = $u[$_obj->uid()];
            		$_list[$k]["group_members"] = $_obj->getVar("group_member_tatol") ;
            		$_list[$k]["group_logo"] = $_obj->logo();
            		$_list[$k]["group_name"] = filterString($_obj->name(),$_queries);
            		$_list[$k]["status"] = $_obj->status();
                }
            }
        }
        $xoopsTpl->assign(array("groups_list"=>$_list,"search_total"=>sprintf(_GP_SEARCH_TOTAL,$_group_total),"search_keys"=>$_queries));
    break;
}
/** 分类群组数 */
$_cat = $category_handler->getCatSelect();
$xoopsTpl->assign("categories",$_cat);
/** 加载搜索form */
$_option = $op;
include_once(dirname(__FILE__)."/include/form.search.php");
include "footer.php";
?>
